"""doc"""

import json
import logging
import requests

from django.http import HttpRequest

from app_common import app_except

logger = logging.getLogger("log")

#
getopendata = "http://api.weixin.qq.com/wxa/getopendata"
unifiedorder = "http://api.weixin.qq.com/_/pay/unifiedorder"


#
def get_phone(request: HttpRequest, body: dict) -> str:
    openid = request.META["HTTP_X_WX_OPENID"]

    getopendata_url = f"{getopendata}?openid={openid}"
    cloudID = body["cloudID"]

    res = requests.post(
        getopendata_url,
        json={"cloudid_list": [cloudID]},
        headers={
            "Content-Type": "application/json",
        },
        # s
        timeout=10,
    )

    #    const data = JSON.parse(body).data_list[0]; // 从回包中获取手机号信息
    #    const phone = JSON.parse(data.json).data.phoneNumber;
    d = json.loads(res.content)
    d_s = json.dumps(d, indent=2)
    logger.info(d_s)

    phone = (
        json.loads(d.get("data_list", [{}])[0].get("json", "{}"))
        .get("data", {})
        .get("phoneNumber", None)
    )
    if not phone:
        raise app_except.NullErr

    return phone
